package com.example.meterialproject.activitys

import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import com.example.meterialproject.activitys.coordinator.CoordinatorDemo1Activity
import com.example.meterialproject.activitys.coordinator.CoordinatorDemo2Activity
import com.example.meterialproject.base.BaseActivity
import com.example.meterialproject.databinding.ActivityCoordinatorBinding
import com.example.meterialproject.exts.*
import com.example.meterialproject.exts.szj.szj
import kotlinx.android.synthetic.main.activity_nested_scroll.view.*

/**
 *
 * @ClassName: CoordinatorActivity
 * @Author: android 超级兵
 * @CreateDate: 4/11/22$ 10:49 AM$
 * TODO CoordinatorLayout 源码分析
 *
 * https://blog.csdn.net/weixin_44819566/article/details/124385289
 */
class CoordinatorActivity : BaseActivity<ActivityCoordinatorBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding.addView.click {
            binding.parentView.addView(createView())
        }

        binding.removeView.click {
            binding.parentView.takeIf {
                it.childCount > 0
            }?.removeViewAt(0) ?: "child = 0" toast this
        }

        /*
         * 作者:android 超级兵
         * 创建时间: 4/22/22 1:08 PM
         * TODO ViewGroup状态监听
         */
        binding.parentView.setOnHierarchyChangeListener(object :
            ViewGroup.OnHierarchyChangeListener {
            // 添加监听
            override fun onChildViewAdded(parent: View, child: View) {
                "添加了一个View" toast this@CoordinatorActivity
                Log.i(TAG, "add parent:${parent.name}\tchild:${child.name}")
            }
            // 删除监听
            override fun onChildViewRemoved(parent: View, child: View) {
                "删除了一个View" toast this@CoordinatorActivity
                Log.i(TAG, "remove parent:${parent.name}\tchild:${child.name}")
            }
        })
        /*
         * 作者:android 超级兵
         * 创建时间: 4/23/22 10:23 AM
         * TODO 监听childView的变化
         */
        binding.parentView.viewTreeObserver.addOnPreDrawListener {
            Log.i("szj变化了", "111")
            return@addOnPreDrawListener true
        }
    }

    private fun createView() = let {
        View(this).apply {
            // 随机颜色
            setBackgroundColor(this@CoordinatorActivity.szj.randomColor())
            // 设置高度
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                50.dp(this@CoordinatorActivity).toInt()
            )
        }
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 4/13/22 7:17 PM
     * TODO 简单demo
     */
    fun onCoordinatorDemo1Click(v: View) {
        jumpActivity(v, CoordinatorDemo1Activity::class.java)
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 4/13/22 7:17 PM
     * TODO 进阶demo
     */
    fun onCoordinatorDemo2Click(v: View) {
        jumpActivity(v, CoordinatorDemo2Activity::class.java)
    }
}